	
	package servlet;
	
	import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

	import enitys.User;
import service.UserManager;
	
	public class LoginServlet extends HttpServlet {
	
		/**
		 * @author dengqin
		 */
		private static final long serialVersionUID = 1L;
	
		public LoginServlet() {
			super();
		}
	
		public void doGet(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			doPost(request, response);
	
		}
	
		/**
		 * LoginServlet
		 * 
		 * @param request
		 *            the request send by the client to the server
		 * @param response
		 *            the response send by the server to the client
		 * @throws ServletException
		 *             if an error occurred
		 * @throws IOException
		 *             if an error occurred
		 */
		public void doPost(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			String username = request.getParameter("username");// 接收输入的用户名
			String userpass = request.getParameter("password");// 接收输入的密码
			int ID = Integer.parseInt(request.getParameter("radio"));
			HttpSession session = request.getSession();// 定义session
				User user = new User();
				user.setUsername(username);
				user.setPassword(userpass);
				
				boolean flag = UserManager.getInstance().Login(username,userpass,ID);
				if (flag==true) {
					session.setAttribute("username", user.getUsername());
					session.setAttribute("password", user.getPassword());
					session.setAttribute("id", user.getS_id());
					session.setAttribute("shenfen", ID);
					System.out.print("shenfen="+ID);
					
					if(ID==0){
						request.getRequestDispatcher("/user.jsp").forward(request, response);
					}
					if(ID==1){
						request.getRequestDispatcher("/admin.jsp").forward(request, response);
					}
				} else {
					request.getRequestDispatcher("/error_log.jsp").forward(request,response);
				}
			}

		}

